package com.example.appointment.repository;

import com.example.appointment.entity.DoctorSchedule;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
public interface DoctorScheduleRepository extends JpaRepository<DoctorSchedule, Long> {
    boolean existsByDoctorIdAndScheduleDate(Long doctorId, LocalDate scheduleDate);
    
    @Query("SELECT ds FROM DoctorSchedule ds WHERE ds.doctorId = :doctorId AND ds.scheduleDate = :date")
    List<DoctorSchedule> findByDoctorIdAndScheduleDate(@Param("doctorId") Long doctorId, @Param("date") LocalDate date);
    
    @Query("SELECT ds FROM DoctorSchedule ds WHERE ds.doctor.department.id = :departmentId AND ds.scheduleDate = :date")
    List<DoctorSchedule> findByDepartmentIdAndScheduleDate(@Param("departmentId") Long departmentId, @Param("date") LocalDate date);
    
    List<DoctorSchedule> findByDoctorIdAndScheduleDateGreaterThanEqual(Long doctorId, LocalDate date);
    
    @Query("SELECT ds FROM DoctorSchedule ds WHERE ds.doctor.department.id = :departmentId AND ds.scheduleDate >= :date")
    List<DoctorSchedule> findByDepartmentIdAndScheduleDateGreaterThanEqual(@Param("departmentId") Long departmentId, @Param("date") LocalDate date);
    
    @Modifying
    @Query("UPDATE DoctorSchedule ds SET ds.availableAppointments = ds.availableAppointments + :change WHERE ds.id = :scheduleId")
    void updateAvailableAppointments(@Param("scheduleId") Long scheduleId, @Param("change") int change);
    
    /**
     * 获取今天及以后的排班，按日期升序排列
     */
    List<DoctorSchedule> findByScheduleDateGreaterThanEqualOrderByScheduleDate(LocalDate date);
    
    /**
     * 获取指定日期之前的排班，按日期降序排列
     */
    @Query(value = "SELECT ds FROM DoctorSchedule ds WHERE ds.scheduleDate < :date ORDER BY ds.scheduleDate DESC")
    List<DoctorSchedule> findByScheduleDateLessThanOrderByScheduleDateDesc(@Param("date") LocalDate date);
    
    /**
     * 获取指定日期之前的排班，按日期降序排列，限制返回数量
     * 使用Pageable参数代替limit参数
     */
    List<DoctorSchedule> findByScheduleDateLessThanOrderByScheduleDateDesc(LocalDate date, Pageable pageable);
    
    /**
     * 使用原生SQL查询
     */
    @Query(value = "SELECT * FROM doctor_schedule WHERE doctor_id = :doctorId AND schedule_date = :date", nativeQuery = true)
    List<DoctorSchedule> findSchedulesByNativeQuery(@Param("doctorId") Long doctorId, @Param("date") String date);

    /**
     * 根据医生ID和日期范围查询排班记录，按日期和时段升序排序
     * @param doctorId 医生ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 排班记录列表
     */
    List<DoctorSchedule> findByDoctorIdAndScheduleDateBetweenOrderByScheduleDateAscPeriodAsc(
        Long doctorId, LocalDate startDate, LocalDate endDate);
} 